home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vg-2.03 / testmenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  6.9 KB  |  387 lines

  1. /*
  2.  * Copyright (C) 1993 by Michael Davidson.
  3.  * All rights reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software
  6.  * and its documentation for any purpose and without fee is hereby
  7.  * granted, provided that the above copyright notice appear in all
  8.  * copies and that both that copyright notice and this permission
  9.  * notice appear in supporting documentation.
  10.  *
  11.  * This software is provided "as is" without express or implied warranty.
  12.  */
  13.  
  14. #include    <stdio.h>
  15. #include    "vg.h"
  16. #include    "video.h"
  17. #include    "kbd.h"
  18.  
  19. STATIC int    test0();
  20. STATIC int    test1();
  21.  
  22. STATIC int    (*Tests[])() =
  23. {
  24.     test0,
  25.     test1,
  26.     0
  27. };
  28.  
  29. STATIC color_t    TestColorMap[256];
  30. STATIC int    TestMode;
  31. STATIC vmode_t    *TestModeInfo;
  32. STATIC char    TestInfo[40];
  33.  
  34. void
  35. testInitColorMap()
  36. {
  37.     color_t    *c;
  38.     int        v;
  39.  
  40.     for (v = 0, c = TestColorMap; v < 256; v += 4, c++)
  41.     {
  42.     c[0].red    = v;    c[0].green    = 0;    c[0].blue    = 0;
  43.     c[64].red    = 0;    c[64].green    = v;    c[64].blue    = 0;
  44.     c[128].red    = 0;    c[128].green    = 0;    c[128].blue    = v;
  45.     c[192].red    = v;    c[192].green    = v;    c[192].blue    = v;
  46.     }
  47. }
  48.  
  49. #define    SUPPORTED_GRAPHICS_MODE(m)                    \
  50.         ( ((m)->flags & (V_MODE_SUPPORTED | V_GRAPHICS_MODE))    \
  51.          == (V_MODE_SUPPORTED | V_GRAPHICS_MODE) )
  52. void
  53. testMenu()
  54. {
  55.     vmode_t    *modes;
  56.     vmode_t    *m;
  57.     int        r;
  58.  
  59.     testInitColorMap();
  60.  
  61.     modes    = vidGetModes();
  62.  
  63.     /*
  64.      * find first supported mode
  65.      */
  66.     for (m = modes; m->flags != 0; m++)
  67.     if (SUPPORTED_GRAPHICS_MODE(m))
  68.         break;
  69.  
  70.     while (m >= modes && m->flags != 0)
  71.     {
  72.     TestMode    = m - modes;
  73.     TestModeInfo    = m;
  74.  
  75.     r = testExecute();
  76.  
  77.     switch (r)
  78.     {
  79.         case F_NEXT:
  80.         while ((++m)->flags != 0)
  81.             if (SUPPORTED_GRAPHICS_MODE(m))
  82.             break;
  83.         break;
  84.  
  85.         case F_PREV:
  86.         while (--m >= modes)
  87.             if (SUPPORTED_GRAPHICS_MODE(m))
  88.             break;
  89.         break;
  90.  
  91.         case F_REDRAW:
  92.         continue;
  93.  
  94.         case F_ESC:
  95.         return;
  96.  
  97.         default:
  98.         imageError("testExecute returned %02x", r);
  99.         break;
  100.  
  101.     }
  102.     }
  103. }
  104.  
  105. int
  106. testExecute()
  107. {
  108.     int        i;
  109.     int        r;
  110.  
  111.     i    = 0;
  112.  
  113.     vidSetMode(TestMode);
  114.     vidSetGamma(100, 100, 100);
  115.  
  116.     while (i >= 0 && Tests[i] != 0)
  117.     {
  118.     vidSetGraphicsRedraw(Tests[i]);
  119.         
  120.     Tests[i]();
  121.  
  122.     switch (r = testEnd())
  123.     {
  124.         case F_NEXT:
  125.         i++;
  126.         break;
  127.  
  128.         case F_PREV:
  129.         i--;
  130.         break;
  131.  
  132.         case F_ESC:
  133.         return F_ESC;
  134.  
  135.         default:
  136.         break;
  137.     }
  138.     }
  139.     return r;
  140. }
  141.  
  142. /*
  143.  * testEnd()
  144.  */
  145. int
  146. testEnd()
  147. {
  148.     int        k;
  149.  
  150.     for (;;)
  151.     {
  152.     k    = kbdGetKey(K_WAIT);
  153.  
  154.     switch (k)
  155.     {
  156.         case K_ENTER:
  157.         case 'n':
  158.         case 'N':
  159.         return F_NEXT;
  160.  
  161.         case 'p':
  162.         case 'P':
  163.         return F_PREV;
  164.  
  165.         case K_ESCAPE:
  166.         return F_ESC;
  167.  
  168.         default:
  169.         break;
  170.     }
  171.     }
  172. }
  173.  
  174. /*
  175.  * test0()
  176.  */
  177. STATIC int
  178. test0()
  179. {
  180.     int    line_width, line_height, box_width, box_height, left, top;
  181.     int    total_width, total_height;
  182.     int        i, j, k, n, y;
  183.     pixel8_t    *pixels;
  184.     int        (*put_pixels)();
  185.  
  186.     line_width    = TestModeInfo->width / 160;
  187.     box_width    = (TestModeInfo->width - line_width * 9) / 8;
  188.     total_width = 9 * line_width + 8 * box_width;
  189.     left    = (TestModeInfo->width - total_width) / 2;
  190.     line_height    = line_width;
  191.     box_height    = (TestModeInfo->height - line_height * 7) / 6;
  192.     total_height= 7 * line_height + 6 * box_height;
  193.     top        = (TestModeInfo->height - total_height) / 2;
  194.  
  195.     if (TestModeInfo->depth > 8)
  196.     {
  197.     line_width    *= 3;
  198.     box_width    *= 3;
  199.     total_width    *= 3;
  200.     left        *= 3;
  201.     put_pixels    = vidPutPixels24;
  202.     pixels        = alloca(TestModeInfo->width * 3);
  203.     memset(pixels, 0, TestModeInfo->width * 3);
  204.     }
  205.     else
  206.     {
  207.     put_pixels    = vidPutPixels8;
  208.     pixels        = alloca(TestModeInfo->width);
  209.     memset(pixels, 0, TestModeInfo->width);
  210.     vidSetPalette(TestColorMap, 256);
  211.     }
  212.  
  213.     vidClear(0);
  214.     /*
  215.      * draw the top line
  216.      */
  217.     y = top;
  218.     for (i = 0; i < line_height; i++, y++)
  219.     {
  220.     memset(pixels + left, 255, total_width);
  221.     put_pixels(0, y, pixels, TestModeInfo->width);
  222.     }
  223.  
  224.     /*
  225.      * draw 6 rows of boxes
  226.      */
  227.     for (n = 0; n < 6; n++)
  228.     {
  229.     for (i = 0; i < box_height; i++, y++)
  230.     {
  231.         pixel8_t    *p = pixels + left;
  232.  
  233.         for (j = line_width; j < total_width; j += line_width)
  234.         for (k = 0; k < box_width; k++)
  235.             p[j++] = 0;
  236.  
  237.         put_pixels(0, y, pixels, TestModeInfo->width);
  238.     }
  239.  
  240.     for (i = 0; i < line_height; i++, y++)
  241.     {
  242.         memset(pixels + left, 255, total_width);
  243.         put_pixels(0, y, pixels, TestModeInfo->width);
  244.     }
  245.     }
  246.  
  247.     testShowInfo();
  248.  
  249.     return 0;
  250. }
  251.  
  252. /*
  253.  * test1()
  254.  */
  255. pixel24_t    p24tab[] =
  256. {
  257.     { 1, 0, 0 },
  258.     { 0, 1, 0 },
  259.     { 0, 0, 1 },
  260.     { 1, 1, 1 }
  261. };
  262.  
  263. STATIC int
  264. test1()
  265. {
  266.     int        h, total_width, step, top, left, i, j, k, l;
  267.  
  268.     h        = TestModeInfo->height / 9;
  269.     total_width    = TestModeInfo->width / 256;
  270.     total_width    *= 256;
  271.     if (total_width == TestModeInfo->width)
  272.     total_width -= 256;
  273.     top        = h;
  274.     left    = (TestModeInfo->width - total_width) / 2;
  275.  
  276.     if (TestModeInfo->depth <= 8)
  277.     {
  278.     int    y;
  279.     int    v;
  280.     pixel8_t    *pixels = alloca(TestModeInfo->width);
  281.  
  282.     vidSetPalette(TestColorMap, 256);
  283.  
  284.     y    = top;
  285.     v    = 0;
  286.     step    = total_width / 64;
  287.  
  288.     memset(pixels, 224, TestModeInfo->width);
  289.  
  290.     for (y = 0; y < top; y++)
  291.         vidPutPixels8(0, y, pixels, TestModeInfo->width);
  292.  
  293.     for (i = 0; i < 4; i++)
  294.     {
  295.         for (j = 0; j < h; j++, y++)
  296.         {
  297.         pixel8_t    *p = pixels + left;
  298.  
  299.         for (k = 0; k < 64; k++)
  300.             for (l = 0; l < step; l++)
  301.             *p++ = v + k;
  302.         vidPutPixels8(0, y, pixels, TestModeInfo->width);
  303.         }
  304.         memset(pixels, 224, TestModeInfo->width);
  305.         for (j = 0; j < h; j++, y++)
  306.         vidPutPixels8(0, y, pixels, TestModeInfo->width);
  307.  
  308.         v += 64;
  309.     }
  310.     memset(pixels, 224, TestModeInfo->width);
  311.  
  312.     while (y < TestModeInfo->height)
  313.         vidPutPixels8(0, y++, pixels, TestModeInfo->width);
  314.     }
  315.     else
  316.     {
  317.     int        y;
  318.     pixel24_t    v, vv;
  319.     pixel24_t    *pixels = alloca(TestModeInfo->width * 3);
  320.  
  321.     step    = total_width / 256;
  322.  
  323.     memset(pixels, 128, TestModeInfo->width * 3);
  324.  
  325.     for (y = 0; y < top; y++)
  326.         vidPutPixels24(0, y, pixels, TestModeInfo->width);
  327.  
  328.     for (i = 0; i < 4; i++)
  329.     {
  330.         vv    = p24tab[i];
  331.  
  332.         for (j = 0; j < h; j++, y++)
  333.         {
  334.         pixel24_t    *p = pixels + left;
  335.  
  336.         v.r    = 0;
  337.         v.g    = 0;
  338.         v.b    = 0;
  339.  
  340.         for (k = 0; k < 256; k++)
  341.         {
  342.             for (l = 0; l < step; l++)
  343.             *p++ = v;
  344.  
  345.             v.r    += vv.r;
  346.             v.g    += vv.g;
  347.             v.b    += vv.b;
  348.         }
  349.         vidPutPixels24(0, y, pixels, TestModeInfo->width);
  350.         }
  351.  
  352.         memset(pixels, 128, TestModeInfo->width * 3);
  353.         for (j = 0; j < h; j++, y++)
  354.         vidPutPixels24(0, y, pixels, TestModeInfo->width);
  355.     }
  356.     memset(pixels, 128, TestModeInfo->width * 3);
  357.  
  358.     while (y < TestModeInfo->height)
  359.         vidPutPixels24(0, y++, pixels, TestModeInfo->width);
  360.     }
  361.  
  362.     testShowInfo();
  363.  
  364.     return 0;
  365. }
  366.  
  367. testShowInfo()
  368. {
  369.     char    buf[120];
  370.     int        len;
  371.     int        x, y;
  372.     font_t    *font        = DefaultFont;
  373.  
  374.     sprintf(buf, "%dx%d-%d",
  375.     TestModeInfo->width, TestModeInfo->height, TestModeInfo->depth);
  376.     len    = strlen(buf);
  377.  
  378.     if (len > TestModeInfo->width / font->f_width)
  379.     len = TestModeInfo->width / font->f_width;
  380.  
  381.     x    = (TestModeInfo->width - font->f_width * len) / 2;
  382.     y    = TestModeInfo->height - font->f_height - 8;
  383.  
  384.     vidDrawText(x, y, buf, len, font);
  385.  
  386. }
  387.